﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="relatorios_dr_r_visao_geral" %>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Visão Geral</title>

	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
	<style type="text/css">
        ${demo.css}
	</style>
</head>
<body>
    <script src="js/highcharts.js" type="text/javascript"></script>
    <script src="js/modules/exporting.js" type="text/javascript"></script>    

    <br />

    <style>
        .myTd
        {            
            padding-left: 10px;            
        }
        
        .green 
        {
            color: Green;
            font-weight: bold;
        }
        
        .red
        {
            color: Red;
            font-weight: bold;
        }
        
    </style>
    <%
        int count = this.AllReports.Count;
        
        string dates = "";
        string incs = "";
        string outs = "";
        
        for (int i = 0; i < count; ++i)
        {
            dates = "";
            incs = "";
            outs = "";
            if (this.AllReports[i] == null || this.AllReports[i].Reports == null || this.AllReports[i].Reports.Count == 0)
            {
                continue;
            }

            string title = "";
            double sumInc = 0;
            double sumOut = 0;
            
            foreach (var rep in this.AllReports[i].Reports)
            {
                if (string.IsNullOrEmpty(rep.Title) == false)
                {
                    title = rep.Title;
                }

                if (string.IsNullOrEmpty(rep.StartDate) == false)
                {
                    string[] theDate = rep.EndDate.Split('/');
                    dates += "'" + theDate[1] + "/" + theDate[2] + "',";
                    incs += "" + (rep.TotalIncoming + "").Replace(',', '.') + ",";
                    outs += "" + (rep.TotalOutcoming + "").Replace(',', '.') + ",";

                    sumInc += rep.TotalIncoming;
                    sumOut += rep.TotalOutcoming;
                }                                               
            }
            if (string.IsNullOrEmpty(dates) == false)
            {
                dates = dates.Substring(0, dates.Length - 1);
            }
            if (string.IsNullOrEmpty(incs) == false)
            {
                incs = incs.Substring(0, incs.Length - 1);
            }
            if (string.IsNullOrEmpty(outs) == false)
            {
                outs = outs.Substring(0, outs.Length - 1);
            }

            // Balanço
            double totalRise_Inc = (sumInc) / this.AllReports[i].Reports.Count;
            double totalRise_Out = (sumOut) / this.AllReports[i].Reports.Count;
            double totalRise_Balance = (sumInc - sumOut) / this.AllReports[i].Reports.Count;

            // Com relação ao último mês
            int lastIndex = this.AllReports[i].Reports.Count - 1;
            double lastMonthRise_Inc = 0;
            double lastMonthRise_Out = 0;
            double lastMonthRise_Balance = 0;

            double firstMonthRise_Inc = 0;
            double firstMonthRise_Out = 0;
            double firstMonthRise_Balance = 0;

            if (lastIndex > 0)
            {
                lastMonthRise_Inc = this.CalcPercentage(this.AllReports[i].Reports[lastIndex - 1].TotalIncoming, this.AllReports[i].Reports[lastIndex].TotalIncoming);
                lastMonthRise_Out = this.CalcPercentage(this.AllReports[i].Reports[lastIndex - 1].TotalOutcoming, this.AllReports[i].Reports[lastIndex].TotalOutcoming);
                lastMonthRise_Balance = this.CalcPercentage(this.AllReports[i].Reports[lastIndex - 1].TotalIncoming - this.AllReports[i].Reports[lastIndex - 1].TotalOutcoming, this.AllReports[i].Reports[lastIndex].TotalIncoming - this.AllReports[i].Reports[lastIndex].TotalOutcoming);

                firstMonthRise_Inc = this.CalcPercentage(this.AllReports[i].Reports[0].TotalIncoming, this.AllReports[i].Reports[lastIndex].TotalIncoming);
                firstMonthRise_Out = this.CalcPercentage(this.AllReports[i].Reports[0].TotalOutcoming, this.AllReports[i].Reports[lastIndex].TotalOutcoming);
                firstMonthRise_Balance = this.CalcPercentage(this.AllReports[i].Reports[0].TotalIncoming - this.AllReports[i].Reports[0].TotalOutcoming, this.AllReports[i].Reports[lastIndex].TotalIncoming - this.AllReports[i].Reports[lastIndex].TotalOutcoming);                
            }
    %>

	        <script type="text/javascript">
	            $(function () {
	                $('#container-<%= i %>').highcharts({
	                    chart: {
	                        type: 'areaspline'
	                    },
	                    title: {
	                        text: '<%= title %>'
	                    },
	                    legend: {
	                        layout: 'vertical',
	                        align: 'left',
	                        verticalAlign: 'top',
	                        x: 150,
	                        y: 100,
	                        floating: true,
	                        borderWidth: 1,
	                        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
	                    },
	                    xAxis: {
	                        categories: [
                                <%= dates %>
                            ],
	                    },
	                    yAxis: {
	                        title: {
	                            text: ''
	                        }
	                    },
	                    tooltip: {
	                        shared: true,
	                        valueSuffix: ' reais'
	                    },
	                    credits: {
	                        enabled: false
	                    },
	                    plotOptions: {
	                        areaspline: {
	                            fillOpacity: 0.5
	                        }
	                    },
	                    series: [{
	                        name: 'Receitas',
	                        data: [<%= incs %>]
	                    }, {
	                        name: 'Despesas',
	                        data: [<%= outs %>]
	                    }]
	                });
	            });
	        </script>
            <div style="width: 30%; height: 310px; float: left; margin-right: 10px; margin-bottom: 10px; border: 1px solid;">
                <div id="container-<%= i %>" style="height: 220px"></div>
                <table style="width: 100%;">
                    <tr style="width: 100%;">
                        <td class="myTd"><strong>Ref. ao Mês Passado</strong></td>
                        <td class="myTd"><strong>Ref. ao Primeiro Mês</strong></td>                        
                    </tr>

                    <tr style="width: 100%;">
                        <td class="myTd">
                            Receita: 
                            <span class="<%= lastMonthRise_Inc > 0 ? "green": "red" %>">
                                <%= lastMonthRise_Inc.ToString("#,##0.00") %> %
                            </span>
                            <br />
                            Despesa:
                            <span class="<%= lastMonthRise_Out >= 0 ? "red": "green" %>">
                                <%= lastMonthRise_Out.ToString("#,##0.00")%> %
                            </span>
                            <br />
                            Balanço:
                            <span class="<%= lastMonthRise_Balance > 0 ? "green": "red" %>">
                                <%= lastMonthRise_Balance.ToString("#,##0.00")%> %
                            </span>
                        </td>

                        <td class="myTd">
                            Receita: 
                            <span class="<%= firstMonthRise_Inc > 0 ? "green": "red" %>">
                                <%= firstMonthRise_Inc.ToString("#,##0.00")%> %
                            </span>
                            <br />
                            Despesa:
                            <span class="<%= firstMonthRise_Out >= 0 ? "red": "green" %>">
                                <%= firstMonthRise_Out.ToString("#,##0.00")%> %
                            </span>
                            <br />
                            Balanço:
                            <span class="<%= firstMonthRise_Balance > 0 ? "green": "red" %>">
                                <%= firstMonthRise_Balance.ToString("#,##0.00")%> %
                            </span>
                        </td>
                    </tr>
                </table>
            </div>            
    <%
            //this.Response.Write("Despesa: " + this.outcomingPair[i].Key + ": " + this.outcomingPair[i].Value);            
            //this.Response.Write("Receita: " + this.incomingPair[i].Key + ": " + this.incomingPair[i].Value);
        }                
    %>
</body>
</html>
